﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace Model
{
	public partial class DlgCableTraySystemType : System.Windows.Forms.Form
	{
		public DlgCableTraySystemType(CableTray cableTray)
		{
			this.InitializeComponent();
			this.targetTray = cableTray;
		}

		private void DlgCableTraySystemType_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			this.cmbSystemType.Items.Add(SystemType.强电.ToString());
			this.cmbSystemType.Items.Add(SystemType.弱电.ToString());
			this.cmbSystemType.Items.Add(SystemType.消防强电.ToString());
			this.cmbSystemType.Items.Add(SystemType.消防弱电.ToString());
			this.LoadDlgData();
            string text = this.targetTray.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.RBS_CTC_SERVICE_TYPE).AsString();
			if (!string.IsNullOrEmpty(text))
			{
				foreach (object obj in this.cmbSystemType.Items)
				{
					if (obj.ToString() == text)
					{
						this.cmbSystemType.SelectedItem = obj;
					}
				}
			}
			if (this.cmbSystemType.SelectedIndex == -1)
			{
				this.cmbSystemType.SelectedIndex = 0;
			}
		}

		private void cmbSystemType_Leave(object sender, EventArgs e)
		{
			string text = this.cmbSystemType.Text;
			if (!this.cmbSystemType.Items.Contains(text) && !this.cableTraySysTypes.Contains(text))
			{
				this.cableTraySysTypes.Add(text);
			}
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTraySystemType", "Params"));
			string attribute = this.xmlHelper.GetAttribute("SystemType");
			if (!string.IsNullOrEmpty(attribute))
			{
				foreach (string text in attribute.Split(new char[]
				{
					'-'
				}))
				{
					if (!this.cmbSystemType.Items.Contains(text))
					{
						this.cmbSystemType.Items.Add(text);
						this.cableTraySysTypes.Add(text);
					}
				}
			}
			attribute = this.xmlHelper.GetAttribute("Applied");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "1")
				{
					this.chkApplied.Checked = true;
					return;
				}
				if (attribute == "0")
				{
					this.chkApplied.Checked = false;
				}
			}
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTraySystemType"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTraySystemType", "Params"));
			string text = "";
			if (this.cableTraySysTypes.Count > 0)
			{
				if (this.cableTraySysTypes.Count > 5)
				{
					for (int i = 0; i <= this.cableTraySysTypes.Count - 5; i++)
					{
						this.cableTraySysTypes.RemoveAt(i);
					}
				}
				foreach (string str in this.cableTraySysTypes)
				{
					text = text + str + "-";
				}
				text = text.Remove(text.Count<char>() - 1);
				this.xmlHelper.SetAttribute("SystemType", text);
			}
			if (this.chkApplied.Checked)
			{
				this.xmlHelper.SetAttribute("Applied", "1");
			}
			else
			{
				this.xmlHelper.SetAttribute("Applied", "0");
			}
			this.xmlHelper.SaveXml("");
		}

		private void DlgCableTraySystemType_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.cmbSystemType.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "系统类型不能为空。");
				this.cmbSystemType.Focus();
				return;
			}
			base.DialogResult = DialogResult.OK;
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableTraySystemType.xml");

		private const string Root = "Root";

		private const string Node = "HYCableTraySystemType";

		private const string ChildNode = "Params";

		private const string CableTraySystemType = "SystemType";

		private const string Applied = "Applied";

		private List<string> cableTraySysTypes = new List<string>();

		private CableTray targetTray;
	}
}
